package com.iweipan.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.iweipan.R;
import com.iweipan.bean.OrderListBean;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by xyb on 2016/12/8.
 */

public class OrderListAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<OrderListBean.ResultBean> strings = new ArrayList<>();


    public OrderListAdapter(Context context) {
        this.context = context;
    }


    public void setData(ArrayList<OrderListBean.ResultBean> strings) {
        this.strings = strings;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return strings.size();
    }

    @Override
    public Object getItem(int position) {
        return strings.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint({"ViewHolder", "SetTextI18n"})
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (viewHolder == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.listview_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        OrderListBean.ResultBean item = (OrderListBean.ResultBean) getItem(position);
        if (item.getReType() == 1) {
            viewHolder.createFee.setVisibility(View.VISIBLE);
            viewHolder.buyPrice.setText(item.getBuyPrice() + "元");
            if (item.getCouponFlag() == 1) {
                viewHolder.createText.setText("[代金券建仓]");
                viewHolder.createFee.setText("手续费0元");
                viewHolder.isQuan.setText("代金券");
            } else {
                viewHolder.createText.setText("[现金建仓]");
                viewHolder.createFee.setText("手续费" + item.getFee() + "元");
                viewHolder.isQuan.setText("现金");
            }
            if (item.getBuyDirection() == 1) {
                viewHolder.createDirection.setText("买空");
            } else if (item.getBuyDirection() == 2) {
                viewHolder.createDirection.setText("买涨");
            }
            viewHolder.createInfo.setText(item.getProDesc() + item.getWeight() + item.getSpec());
            viewHolder.createTime.setText(item.getAddTime());
            viewHolder.createMoney.setText("花费 " + item.getBuyMoney() + "元");

        } else if (item.getReType() == 2) {
            viewHolder.createFee.setVisibility(View.GONE);

            viewHolder.buyPrice.setText(item.getSellPrice() + "元");
            if (item.getCouponFlag() == 1) {
                viewHolder.isQuan.setText("代金券");
            } else {
                viewHolder.isQuan.setText("现金");
            }


            if (item.getOrderType() == 3) {
                viewHolder.createText.setText("[手动平仓]");
            } else if (item.getOrderType() == 4) {
                viewHolder.createText.setText("[止盈平仓]");
            } else if (item.getOrderType() == 5) {
                viewHolder.createText.setText("[止损平仓]");
            } else if (item.getOrderType() == 6) {
                viewHolder.createText.setText("[爆仓平仓]");
            } else if (item.getOrderType() == 7) {
                viewHolder.createText.setText("[休市平仓]");
            }


            if (item.getBuyDirection() == 1) {
                viewHolder.createDirection.setText("买空");
            } else if (item.getBuyDirection() == 2) {
                viewHolder.createDirection.setText("买涨");
            }
            viewHolder.createInfo.setText(item.getProDesc() + item.getWeight() + item.getSpec());
            viewHolder.createTime.setText(item.getSellTime());
            java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");
            viewHolder.createMoney.setText("盈利  " + df.format(item.getPlAmount() - item.getFee())+"元");


        } else {

        }


        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.create_text)
        TextView createText;
        @BindView(R.id.create_info)
        TextView createInfo;
        @BindView(R.id.create_direction)
        TextView createDirection;
        @BindView(R.id.buy_price)
        TextView buyPrice;
        @BindView(R.id.isQuan)
        TextView isQuan;
        @BindView(R.id.create_time)
        TextView createTime;
        @BindView(R.id.create_money)
        TextView createMoney;
        @BindView(R.id.create_fee)
        TextView createFee;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
